#ifndef __M_CHANNEL_H__
#define __M_CHANNEL_H__
#include "muduo/net/TcpConnection.h"
#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "../mqcommon/mq_logger.hpp"
#include "../mqcommon/mq_helper.hpp"
#include "../mqcommon/mq_msg.pb.h"
#include "../mqcommon/mq_proto.pb.h"
#include "../mqcommon/mq_threadpool.hpp"
#include "mq_consumer.hpp"
#include "mq_host.hpp"
#include "mq_route.hpp"
namespace bitmq
{
    using ProtobufCodecPtr = std::shared_ptr<ProtobufCodec>;
    //----------------------------请求类型----------------
    // 信道的一个打开与关闭
    using openChannelRequestPtr = std::shared_ptr<openChannelRequest>;
    using closeChannelRequestPtr = std::shared_ptr<closeChannelRequest>;
    // 交换机的声明与删除
    using declareExchangeRequestPtr = std::shared_ptr<declareExchangeRequest>;
    using deleteExchangeRequestPtr = std::shared_ptr<deleteExchangeRequest>;
    // 队列的声明与删除
    using declareQueueRequestPtr = std::shared_ptr<declareQueueRequest>;
    using deleteQueueRequestPtr = std::shared_ptr<deleteQueueRequest>;
    // 队列的绑定与解除绑定
    using queueBindingRequestPtr = std::shared_ptr<queueBindingRequest>;
    using queueUnBindingRequestPtr = std::shared_ptr<queueUnBindingRequest>;
    // 消息的发布
    using basicPublishRequestPtr = std::shared_ptr<basicPublishRequest>;
    // 消息的确认
    using basicAckRequestPtr = std::shared_ptr<basicAckRequest>;
    // 队列的订阅
    using basicConsumeRequestPtr = std::shared_ptr<basicConsumeRequest>;
    // 订阅的取消
    using basicCancelRequestPtr = std::shared_ptr<basicCancelRequest>;

    class Channel
    {
    public:
        using ptr = std::shared_ptr<Channel>;
        Channel(const std::string &id,
                const VirtualHost::ptr &host,
                const ConsumerManager::ptr &cmp,
                const ProtobufCodecPtr &codec,
                const muduo::net::TcpConnectionPtr &conn,
                const threadpool::ptr &pool)
            : _cid(id), _conn(conn), _codec(codec), _cmp(cmp), _host(host), _pool(pool)
        {
            DLOG("new Channel: %p", this);
        }

        ~Channel()
        {
            if (_consumer.get() != nullptr)
            {
                _cmp->remove(_consumer->tag, _consumer->qname);
            }
            DLOG("delete Channel: %p", this);
        }

        //-------------------------交换机-------------------------------
        // 交换机的声明与删除
        void declareExchange(const declareExchangeRequestPtr &req)
        {
            bool ret = _host->declareExchange(req->exchange_name(), req->exchange_type(),
                                              req->durable(), req->auto_delete(), req->args());
            // 生成响应
            return basicResponse(ret, req->rid(), req->cid());
        }
        void deleteExchange(const deleteExchangeRequestPtr &req)
        {
            _host->deleteExchange(req->exchange_name());
            return basicResponse(true, req->rid(), req->cid());
        }
        //-------------------------队列-------------------------------
        // 队列的声明与删除
        void declareQueue(const declareQueueRequestPtr &req)
        {
            bool ret = _host->declareQueue(req->queue_name(), req->durable(), req->exclusive(), req->auto_delete(), req->args());
            if (ret == false)
            {
                return basicResponse(false, req->rid(), req->cid());
            }
            // 初始化队列的消费者管理句柄
            _cmp->initQueueConsumer(req->queue_name());
            return basicResponse(true, req->rid(), req->cid());
        }
        void deleteQueue(const deleteQueueRequestPtr &req)
        {
            _cmp->destroyQueueConsumer(req->queue_name()); // 删除改队列的所有消费者
            _host->deleteQueue(req->queue_name());
            return basicResponse(true, req->rid(), req->cid());
        }
        //-------------------------绑定-------------------------------
        // 队列的绑定与解绑
        void queueBind(const queueBindingRequestPtr &req)
        {
            DLOG("收到一个绑定请求，请求key:%s", req->binding_key().c_str());
            bool ret = _host->bind(req->exchange_name(), req->queue_name(), req->binding_key());
            return basicResponse(ret, req->rid(), req->cid());
        }
        void queueUnBind(const queueUnBindingRequestPtr &req)
        {
            _host->unBind(req->exchange_name(), req->queue_name());
            return basicResponse(true, req->rid(), req->cid());
        }
        //-------------------------消息-------------------------------
        // 消息的发布
        void basicPublish(const basicPublishRequestPtr &req)
        {
            // 1.获取并判断交换机是否存在
            auto ep = _host->selectExchange(req->exchange_name());
            if (ep.get() == nullptr)
            {
                return basicResponse(false, req->rid(), req->cid());
            }
            // 2.进行交换路由,判断消息可以发布到交换机绑定的哪些队列中
            MsgQueueBindingMap mqbm = _host->ExchangeBindings(req->exchange_name());
            BasicProperties *properties = nullptr;

            std::string routing_key;
            if (req->has_properties())
            {
                properties = req->mutable_properties();
                routing_key = properties->routing_key();
            }
            for (auto &binding : mqbm)
            {
                if (Route::route(ep->type, routing_key, binding.second->binding_key))
                {
                    // 3.将消息添加到队列中（添加消息的管理）
                    _host->basicPublish(binding.first, properties, req->body());
                    // 4.向线程池中添加一个消费消费任务（向指定队列的订阅者去推送消息--线程池完成）
                    auto task = std::bind(&Channel::consume, this, binding.first);
                    _pool->push(task);
                }
            }
            return basicResponse(true, req->rid(), req->cid());
        }
        // 消息的确认
        void basicAck(const basicAckRequestPtr &req)
        {
            _host->basicAck(req->queue_name(), req->message_id());
            return basicResponse(true, req->rid(), req->cid());
        }
        //-------------------------订阅-------------------------------
        // 订阅队列的消息
        void basicConsume(const basicConsumeRequestPtr &req)
        {
            // 1.判断队列是否存在
            bool ret = _host->existsQueue(req->queue_name());
            if (ret == false)
            {
                return basicResponse(false, req->rid(), req->cid());
            }
            // 2.创建队列的消费者
            auto cb = std::bind(&Channel::callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
            _consumer = _cmp->create(req->consumer_tag(), req->queue_name(), req->auto_ack(), cb);
            // 创建了消费者之后，当前的channel角色就是一个消费者
            return basicResponse(true, req->rid(), req->cid());
        }
        // 取消订阅
        void basicCancel(const basicCancelRequestPtr &req)
        {
            _cmp->remove(req->consumer_tag(), req->queue_name());
            return basicResponse(true, req->rid(), req->cid());
        }

    private:
        void callback(const std::string tag, const BasicProperties *bp, const std::string &body)
        {
            // 创建应答，发送给channel对应的客户端
            basicConsumeResponse resp;
            resp.set_cid(_cid);
            resp.set_body(body);
            resp.set_consumer_tag(tag);
            if (bp)
            {
                resp.mutable_properties()->set_id(bp->id());
                resp.mutable_properties()->set_delivery_mode(bp->delivery_mode());
                resp.mutable_properties()->set_routing_key(bp->routing_key());
            }
            _codec->send(_conn, resp);
        }
        void consume(const std::string &qname)
        {
            // 指定队列消费消息
            // 1.从队列中取出消息
            MessagePtr mp = _host->basicConsume(qname);
            // 2.从队列订阅者中取出一个订阅者
            Consumer::ptr cp = _cmp->choose(qname);
            if (cp.get() == nullptr)
            {
                DLOG("执行消费者任务失败，%s 队列没有消费者！", qname.c_str());
                return;
            }
            // 3.调用订阅者对应的消息处理函数，实现消息推送
            cp->callbalk(cp->tag, mp->mutable_payload()->mutable_properties(), mp->payload().body());

            // 4.判断如果订阅者是自动确认，则不需要等待确认，直接删除消息，否则需要外部收到消息确认后再删除
            if (cp->auto_ack)
                _host->basicAck(qname, mp->payload().properties().id());
        }
        void basicResponse(bool ok, const std::string &rid, const std::string &cid)
        {
            basicCommonResponse resp;
            resp.set_rid(rid);
            resp.set_cid(cid);
            resp.set_ok(ok);
            _codec->send(_conn, resp);
        }

    private:
        std::string _cid;                   // 信道标识
        Consumer::ptr _consumer;            // 消费者句柄
        muduo::net::TcpConnectionPtr _conn; // 连接句柄
        ProtobufCodecPtr _codec;            // 协议处理器句柄
        ConsumerManager::ptr _cmp;          // 消费者管理句柄（以队列为单元）
        VirtualHost::ptr _host;
        threadpool::ptr _pool; // 线程池
    };

    class ChannelManager
    {
    public:
        using ptr = std::shared_ptr<ChannelManager>;
        ChannelManager() {}
        // 添加一个信道
        bool openChannel(const std::string &id,
                         const VirtualHost::ptr &host,
                         const ConsumerManager::ptr &cmp,
                         const ProtobufCodecPtr &codec,
                         const muduo::net::TcpConnectionPtr &conn,
                         const threadpool::ptr &pool)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            auto it = _channels.find(id);
            if (it != _channels.end())
            {
                // 信道已经存在
                DLOG("信道已经存在");
                return false;
            }
            auto channel = std::make_shared<Channel>(id, host, cmp, codec, conn, pool);
            _channels.insert(std::make_pair(id, channel));
            return true;
        }
        // 关闭一个信道
        void closeChannel(const std::string &id)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            _channels.erase(id);
        }

        Channel::ptr getChannel(const std::string &id)
        {
            std::unique_lock<std::mutex> lock(_mutex);
            auto it = _channels.find(id);
            if (it == _channels.end())
            {
                return Channel::ptr();
            }
            return it->second;
        }

    private:
        std::mutex _mutex;
        std::unordered_map<std::string, Channel::ptr> _channels;
    };
}

#endif